//
//  MethodOfPaymentView.m
//  BaseFrame
//
//  Created by 吕金状 on 2017/8/9.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "MethodOfPaymentView.h"
#import "MethodOfPaymentViewTableViewCell.h"

@interface MethodOfPaymentView ()<UITableViewDelegate , UITableViewDataSource>
{
    NSString *chooseStrong;
}
Copy void(^selectedHandle)(NSString *str);
Strong UITableView *tableView;
Strong UIView *headView;
Strong UIView *footView;
Strong NSMutableArray *dataArray; /**<*/

@end

@implementation MethodOfPaymentView

- (instancetype)initWithFrame:(CGRect)frame selectedHandle:(void (^)(NSString *))selectedHandle{
    if (self = [super initWithFrame:frame]) {
        self.alpha = 0;
        _selectedHandle = selectedHandle;
        chooseStrong = @"";
        UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        backButton.backgroundColor = [UIColor blackColor];
        backButton.alpha = .6;
        backButton.titleLabel.font = DEF_FontSize_13;
        [backButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:backButton];
        [self addSubview:self.tableView];
    }
    return self;
}

#pragma mark - init

- (UIView *)headView{
    if (!_headView) {
        _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 120 / WIDTH_5S_SCALE, 31 / WIDTH_5S_SCALE)];
        _headView.backgroundColor = getColor(mainColor);
        UILabel *titleLabel  = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 120 / WIDTH_5S_SCALE, 31 / WIDTH_5S_SCALE)];
        titleLabel.font = DEF_FontSize_14;
        titleLabel.textColor = getColor(@"ffffff");
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.text = @"支付方式";
        [_headView addSubview:titleLabel];
    }
    return _headView;
}

- (UIView *)footView{
    if (!_footView) {
        _footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 120 / WIDTH_5S_SCALE, 50 / WIDTH_5S_SCALE)];

        UIButton *typeButton = [[UIButton alloc]initWithFrame:CGRectMake((SCREEN_WIDTH - 120 / WIDTH_5S_SCALE - 64 / WIDTH_5S_SCALE) / 2, (50 / WIDTH_5S_SCALE - 21 / WIDTH_5S_SCALE) / 2, 64 / WIDTH_5S_SCALE, 21 / WIDTH_5S_SCALE)];
        typeButton.layer.masksToBounds = YES;
        typeButton.layer.cornerRadius = 21 / WIDTH_5S_SCALE / 2;
        typeButton.titleLabel.font = DEF_FontSize_14;
        [typeButton setTitle:@"确定" forState:UIControlStateNormal];
        [typeButton setTitleColor:getColor(@"ffffff") forState:UIControlStateNormal];
        typeButton.backgroundColor = getColor(mainColor);
        [typeButton addTarget:self action:@selector(typeButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [_footView addSubview:typeButton];
    }
    return _footView;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(60 / WIDTH_5S_SCALE, 150 / WIDTH_5S_SCALE, SCREEN_WIDTH - 120 / WIDTH_5S_SCALE, 200 / WIDTH_5S_SCALE) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.sectionFooterHeight = .01;
        _tableView.sectionHeaderHeight = .01;
        _tableView.tableFooterView = self.footView;
        _tableView.tableHeaderView = self.headView;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.backgroundColor = getColor(@"ffffff");
        _tableView.layer.masksToBounds = YES;
        _tableView.layer.cornerRadius = 5;
    }
    return _tableView;
}

#pragma mark - click

- (void)typeButtonClick{
   
    if ([chooseStrong isEqualToString:@""]) {
        [self makeToast:@"请选择支付方式" duration:1 position:CSToastPositionCenter];
      
        return;
    }
     [self dismiss];
    if (_selectedHandle) {
        _selectedHandle(chooseStrong);
    }
}

#pragma mark - <UITableViewDelegate , UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MethodOfPaymentViewTableViewCell *cell = [MethodOfPaymentViewTableViewCell cellForTableView:tableView];
    cell.indexPath = indexPath;
    if (![chooseStrong isEqualToString:@""]) {
        cell.chooseString = chooseStrong;
    }
    cell.model = self.dataArray[indexPath.row];
    //cell.accessoryType = indexPath.row == 3 ?  UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    chooseStrong = [NSString stringWithFormat:@"%d",(int)indexPath.row];
    [self.tableView reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44 / WIDTH_5S_SCALE;
}

//出现
-(void)show{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 1;
    }];
}

//消失
-(void)dismiss{
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 0 ;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}


#pragma mark - data

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray  = @[@{@"title":@"会员卡支付"},
                        @{@"title":@"支付宝支付"},
                        @{@"title":@"微信支付"}
                        ].mutableCopy;
    }
    return _dataArray;
}


@end
